package com.herui.iot.data.eval.source;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.herui.sdyu_lib.http.callback.Callback;
import com.herui.sdyu_lib.utils.JsonUtils;
import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sport_care.data.BaseCallBack;
import com.herui.sport_care.data.RepositoryFactory;
import com.herui.sport_care.data.eval.AgreementInfo;
import com.herui.sport_care.data.eval.BetaSchemeInfo;
import com.herui.sport_care.data.eval.EvalDetail;
import com.herui.sport_care.data.eval.EvalHostoryInfo;
import com.herui.sport_care.data.eval.EvalInfo;
import com.herui.sport_care.data.eval.OrganTreeInfo;
import com.herui.sport_care.data.eval.TestUserInfo;
import com.herui.sport_care.data.user.BasePageCallBack;
import com.herui.sport_care.data.user.UserInfo;
import com.herui.sport_care.request.EvalRequest;
import com.herui.sport_care.request.SportPageResponse;
import com.herui.sport_care.request.SportResponse;
import com.herui.sport_care.request.UserRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.herui.sport_care.config.ServerConfig.retrofitCall;

/**
 * Created by amanda.shan on 2021/9/30.
 */
public class EvalRepository implements EvalSource {

    private static EvalRepository instance;

    public static EvalRepository getInstance() {
        if (instance == null) {
            synchronized (EvalRepository.class) {
                if (instance == null) {
                    instance = new EvalRepository();
                }
            }
        }
        return instance;
    }

    // 测试方案
    private List<BetaSchemeInfo> betaSchemeInfos;
    // 测试协议
    private Map<Long, List<AgreementInfo>> agreementInfoMap = new HashMap<>();
    // 受试组别
    private OrganTreeInfo organTreeInfo;

    public void clearAll(){

    }

    @Override
    public void getEvalDay(Callback.CommonCallback<List<EvalInfo>> callback) {

        Map<String, Object> map = new HashMap<>();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        map.put("evaluationDate", format.format(new Date()));
        int organId = RepositoryFactory.getInstance().getUserSource().getUserInfo().getOrganId();
        map.put("organId", organId);

        retrofitCall.call(retrofitCall.conver(EvalRequest.class).getEvalDay(map),
                new BaseCallBack<List<EvalInfo>>(callback) {
                    @Override
                    public SportResponse<List<EvalInfo>> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<List<EvalInfo>>>() {
                                });
                    }
                });
    }

    @Override
    public void getBetaScheme(Callback.CommonCallback<List<BetaSchemeInfo>> callback) {
        if (betaSchemeInfos != null){
            callback.onSuccess(betaSchemeInfos);
            return;
        }

        retrofitCall.call(retrofitCall.conver(EvalRequest.class).getBetaScheme(),
                new BaseCallBack<List<BetaSchemeInfo>>(callback) {
                    @Override
                    public SportResponse<List<BetaSchemeInfo>> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<List<BetaSchemeInfo>>>() {
                                });
                    }

                    @Override
                    public void onResult(List<BetaSchemeInfo> betaSchemeInfos) {
                        EvalRepository.this.betaSchemeInfos = betaSchemeInfos;
                    }
                });
    }

    @Override
    public void getAgreement(Long betaSchemeId, Callback.CommonCallback<List<AgreementInfo>> callback) {
        List<AgreementInfo> agreementInfos = agreementInfoMap.get(betaSchemeId);
        if (agreementInfos != null){
            callback.onSuccess(agreementInfos);
            return;
        }
        retrofitCall.call(retrofitCall.conver(EvalRequest.class).getAgreement(betaSchemeId),
                new BaseCallBack<List<AgreementInfo>>(callback) {
                    @Override
                    public SportResponse<List<AgreementInfo>> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<List<AgreementInfo>>>() {
                                });
                    }

                    @Override
                    public void onResult(List<AgreementInfo> betaSchemeInfos) {
                        agreementInfoMap.put(betaSchemeId, agreementInfos);
                    }
                });

    }

    @Override
    public void getOrganTreeList(Integer organId, Callback.CommonCallback<OrganTreeInfo> callback) {
        retrofitCall.call(retrofitCall.conver(EvalRequest.class).getOrganTreeList(organId),
                new BaseCallBack<OrganTreeInfo>(callback) {
                    @Override
                    public SportResponse<OrganTreeInfo> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<OrganTreeInfo>>() {
                                });
                    }

                    @Override
                    public void onResult(OrganTreeInfo organTreeInfo) {
//                        EvalRepository.this.organTreeInfo = organTreeInfo;
                    }
                });
    }

    @Override
    public void getHistoryEval(Map<String, Object> map, Callback.CommonCallback<List<EvalHostoryInfo>> callback) {
        retrofitCall.call(retrofitCall.conver(EvalRequest.class).getHistoryEval(map),
                new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        LogUtils.d(result);
                        SportPageResponse<List<EvalHostoryInfo>> apiResponse;
                        try {
                            apiResponse =  JsonUtils.parseObject(result,
                                    new TypeReference<SportPageResponse<List<EvalHostoryInfo>>>() {
                                    });;
                            if (apiResponse.getSuccess()) {
                                List<EvalHostoryInfo> t = JsonUtils.parseObject(apiResponse.getResult().getContent().toString(),
                                        new TypeReference<List<EvalHostoryInfo>>() {
                                        });
                                apiResponse.getResult().setContent(t);
                                callback.onSuccess((List<EvalHostoryInfo>) apiResponse.getResult().getContent());
                            } else {
                                callback.onError(null, "%s", apiResponse.getMsg());
                            }
                        } catch (Exception e) {
                            callback.onError(e, "", "");
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable throwable, String s, String s1) {
                        callback.onError(throwable, s + ":%s", s1);
                    }
                });
    }

    @Override
    public void getEvalDetail(Integer evalId, Callback.CommonCallback<EvalDetail> callback) {
        retrofitCall.call(retrofitCall.conver(EvalRequest.class).getEvalDetail(evalId),
                new BaseCallBack<EvalDetail>(callback) {
                    @Override
                    public SportResponse<EvalDetail> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<EvalDetail>>() {
                                });
                    }

                });
    }

    @Override
    public void rateePageList(Map<String, Object> map, Callback.CommonCallback<List<TestUserInfo>> callback) {
        retrofitCall.call(retrofitCall.conver(EvalRequest.class).rateePageList(map),
                new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        LogUtils.d(result);
                        SportPageResponse<List<TestUserInfo>> apiResponse;
                        try {
                            apiResponse =  JsonUtils.parseObject(result,
                                    new TypeReference<SportPageResponse<List<TestUserInfo>>>() {
                                    });;
                            if (apiResponse.getSuccess()) {
                                List<TestUserInfo> t = JsonUtils.parseObject(apiResponse.getResult().getContent().toString(),
                                        new TypeReference<List<TestUserInfo>>() {
                                        });
                                apiResponse.getResult().setContent(t);
                                callback.onSuccess((List<TestUserInfo>) apiResponse.getResult().getContent());
                            } else {
                                callback.onError(null, "%s", apiResponse.getMsg());
                            }
                        } catch (Exception e) {
                            callback.onError(e, "", "");
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable throwable, String s, String s1) {
                        callback.onError(throwable, s + ":%s", s1);
                    }
                });
    }

    @Override
    public void calculatePower(Integer userId, Callback.CommonCallback<Double> callback) {
        retrofitCall.call(retrofitCall.conver(EvalRequest.class).calculatePower(userId),
                new BaseCallBack<Double>(callback) {
                    @Override
                    public SportResponse<Double> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<Double>>() {
                                });
                    }

                });
    }

    @Override
    public void create(Map<String, Object> map, Callback.CommonCallback<String> callback) {
        retrofitCall.call(retrofitCall.conver(EvalRequest.class).create(map),
                new BaseCallBack<String>(callback) {
                    @Override
                    public SportResponse<String> getApiResponse(String s) throws Exception {
                        return JsonUtils.parseObject(s,
                                new TypeReference<SportResponse<String>>() {
                                });
                    }

                });
    }

}
